<template>
  <section>
    <el-container>
      <el-header>
        <schoolInfoSearch :search="search" :filters="filters" :add="add" :killSelection="killSelection"/>
      </el-header>
      <el-main>
        <el-table :data="sheet.rows" highlight-current-row v-loading="sheet.loading" stripe="stripe" border="border"
                  ref="multipleTable"
                  @sort-change="sortChange" style="width: 100%;" max-height="690" :row-style="tableRowStyle">
          <el-table-column type="selection" width="55"/>

          <el-table-column prop="name" label="学校名称" min-width="150" sortable="sortable" :show-overflow-tooltip="true"
                           header-align="center"/>

          <el-table-column prop="contactsName" label="联系人" min-width="150" sortable="sortable"
                           :show-overflow-tooltip="true" header-align="center"/>

          <el-table-column prop="contactsInformation" label="联系方式" min-width="150" sortable="sortable"
                           :show-overflow-tooltip="true" header-align="center"/>

          <el-table-column prop="contactsAddress" label="地址" min-width="150" sortable="sortable"
                           :show-overflow-tooltip="true" header-align="center"/>

          <el-table-column prop="maxStudentCount" label="学生数量限制" min-width="150" sortable="sortable"
                           :show-overflow-tooltip="true" header-align="center"/>

          <el-table-column prop="remark" label="备注" min-width="150" sortable="sortable" :show-overflow-tooltip="true"
                           header-align="center"/>

          <el-table-column prop="classNumber" label="班级流水号" min-width="150" sortable="sortable"
                           :show-overflow-tooltip="true" header-align="center"/>

          <el-table-column prop="isEnable" label="状态" width="120" align="center">
            <template slot-scope="scope">
              <el-tag :type="['','success','info','warning','danger'][['0','1'].indexOf(scope.row.isEnable+ '')%5]">
                {{['禁用','启用'][['0','1'].indexOf(scope.row.isEnable+ '')]}}
              </el-tag>
            </template>
          </el-table-column>


          <el-table-column prop="createTime" label="时间" width="200" align="center" sortable="sortable"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-icon name="time"></el-icon>
              <span style="margin-left: 10px">{{ formatDate(scope.row.createTime) }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" width="220" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">

              <el-button size="small" type="text" @click="showSchoolQuestion(scope.row)">试题</el-button>
              <el-button size="small" type="text" @click="showSchoolCourse(scope.row)">课程</el-button>
              <el-button size="small" type="text" @click="showSchoolResource(scope.row)">资源</el-button>
              <el-dropdown @command="handleCommand">
           <span class="el-dropdown-link">
              <el-button size="small" type="text">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
          </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{'method':'showSchoolInfoInfo','args':scope.row}">编辑</el-dropdown-item>
                  <el-dropdown-item :command="{'method':'killSchoolInfoInfo','args':scope.row.id}">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <lsz-pagination :getList="getList" :sheet="sheet"/>
    </el-container>
    <SchoolCourse ref="schoolCourse" :caption="'公开课程'"/>
    <SchoolInfoEdit ref="schoolInfoEdit" :search="search"/>
    <SchoolResource ref="schoolResource" :caption="'公开资源'"/>
    <SchoolQuestion ref="schoolQuestion" :caption="'公开试题'"/>
  </section>
</template>

<script>
  import LszPagination from '@/components/common/lsz-pagination.vue';
  import SchoolInfoSearch from './SchoolInfoSearch.vue';
  import SchoolInfoEdit from './SchoolInfoEdit.vue';
  import SchoolResource from './SchoolResource.vue';
  import SchoolCourse from './SchoolCourse.vue';
  import SchoolQuestion from './SchoolQuestion.vue';


  let data = () => {
    return {
      filters: {},
      sheet: {
        rows: [],
        total: 0,
        pageNum: 1,
        pageSize: 20,
        sort: 'createTime',
        order: 'desc',
        loading: false
      },
    }
  }
  let getList = function () {
    let param = {
      pageNum: this.sheet.pageNum,
      pageSize: this.sheet.pageSize,
      sort: this.sheet.sort,
      order: this.sheet.order,
      query: this.filters
    }
    this.sheet.loading = true
    this.$http.post('/schoolInfoController/getSchoolInfoList', param).then(res => {

      let response = res.data.data;

      this.sheet.total = response.total
      this.sheet.rows = response.content
      this.sheet.loading = false
    }).catch(() => this.sheet.loading = false)
  }

  export default {
    data: data,
    components: {
      LszPagination,
      SchoolInfoSearch,
      SchoolInfoEdit,
      SchoolResource,
      SchoolCourse,
      SchoolQuestion
    },
    methods: {
      search: function () {
        this.sheet.pageNum = 1
        this.getList()
      },
      killSelection: function () {
        let rows = this.$refs.multipleTable.store.states.selection;
        if (rows && rows.length > 0) {
          let arr = [];
          for (let row of rows) {
            arr.push(row.id);
          }
          this.killSchoolInfoInfo(arr.join(","));
        }
      },
      handleCommand: function (param) {
        this[param.method](param.args)
      },
      tableRowStyle({row, rowIndex}) {
        if (row.color) {
          return 'color: ' + row.color;
        }
      },
      showSchoolQuestion(row) {
        this.$refs.schoolQuestion.show(row.id, row.name)
      },
      showSchoolCourse(row) {
        this.$refs.schoolCourse.show(row.id, row.name)
      },
      showSchoolResource(row) {
        this.$refs.schoolResource.show(row.id, row.name)
      },
      killSchoolInfoInfo: function (ids) {
        let vm = this;
        vm.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          vm.$http.post('/schoolInfoController/deleteSchoolInfo', {id: ids}).then(res => {
            if (!res.data.success) {
              vm.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error'
              });
              return
            }
            vm.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.getList();
          })
        }).catch(() => {

        })
      },
      showSchoolInfoInfo: function (row) {
        this.$refs.schoolInfoEdit.show(row);
      },
      add: function () {
        this.$refs.schoolInfoEdit.showAdd();
      },
      formatDate: function (d, format) {
        if (!d) {
          return "";
        }
        if (!format) {
          format = 'YYYY-MM-DD HH:mm:ss';
        }
        return this.$moment(d).format(format)
      },
      getList,
      sortChange: function (d) {
        this.sheet.sort = d && d.prop
        this.sheet.order = d && d.order && d.order.indexOf('asc') >= 0 ? 'ASC' : 'DESC'
        this.search()
      },
    },
    mounted: function () {
      this.search()
    },
    name: "SchoolInfo"
  }
</script>

<style scoped>
</style>

